﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Service;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.AdverseEvent
{
    public class AdverseEventModeController : BaseController
    {
        // GET: AdverseEvent/AdverseEventMode
        public ActionResult Index(int typeID = 0, string typeName = "")
        {
            ViewBag.TypeID = typeID;
            ViewBag.TypeName = typeName;
            return View();
        }

        /// <summary>
        /// 发生方式列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            int pageIndex = DataConvert.StrToInt(Request["page"], 1);
            int pageSize = DataConvert.StrToInt(Request["pageSize"], ConfigHelper.GetConfigInt("pagesize"));
            string whereSql = GetWhereSql();
            SelectBuilder data = new SelectBuilder();
            data.Having = "";
            data.GroupBy = "";
            data.OrderBy = "Sort ASC";
            data.From = "AdverseEventMode";
            data.Select = "ID,ModeName,InputType,Options,Sort";
            data.WhereSql = whereSql;
            data.PagingCurrentPage = pageIndex;
            data.PagingItemsPerPage = pageSize;
            int total = 0;
            DataTable dt = BaseService<AdverseEventType>.GetQueryManyForPage_Proc(data, out total);
            var result = new { Total = total, Rows = dt };
            return JsonDate(result);
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private string GetWhereSql()
        {
            int typeID = DataConvert.StrToInt(Request["typeID"]);
            string whereSql = "1 = 1";
            if (typeID > 0)
            {
                whereSql += string.Format(" and TypeID = {0}", typeID);
            }
            else
            {
                whereSql = "1 != 1";
            }
            return whereSql;
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0)
        {
            AdverseEventMode adverseEventMode = new AdverseEventMode();
            if (id > 0)
            {
                adverseEventMode = AdverseEventModeService.GetQuerySingleByID(id);
            }
            ViewBag.AdverseEventMode = adverseEventMode;
            return View();
        }

        /// <summary>
        /// 添加/编辑发生方式
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(AdverseEventMode adverseEventMode)
        {
            OperateResult resultInfo = AdverseEventModeManager.Save(adverseEventMode);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 删除发生方式
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            OperateResult resultInfo = AdverseEventModeManager.Delte(id);
            return JsonDate(resultInfo);
        }
    }
}